package com.eningqu.aipen.ui.login;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

import androidx.databinding.ObservableField;

import com.eningqu.aipen.demo.R;
import com.eningqu.aipen.demo.databinding.ActivityLoginBinding;
import com.eningqu.aipen.logic.model.login.LoginResponseBean;
import com.eningqu.aipen.logic.model.login.LoginUserBean;
import com.eningqu.aipen.logic.network.Apis;
import com.eningqu.aipen.logic.network.BaseRetrofitUtils;
import com.eningqu.aipen.logic.network.SchedulerTransformer;
import com.eningqu.aipen.ui.MainActivity;
import com.eningqu.aipen.ui.base.BaseActivity;
import com.eningqu.aipen.utils.DigestUtils;
import com.eningqu.aipen.utils.GlobalConfig;
import com.eningqu.aipen.utils.SPUtil;
import com.eningqu.aipen.utils.ToastUtils;
import com.kongzue.dialogx.dialogs.PopTip;

import java.util.Objects;

import io.reactivex.functions.Consumer;

/**
 * Package:com.eningqu.aipen.ui.login
 * Author:starr
 * Time:2023/9/14  11:04
 * Description:
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding> {

    private boolean isRegister = false;

    ObservableField<String> phone = new ObservableField<>();
    //    ObservableField<String> code = new ObservableField<>();
    ObservableField<String> password = new ObservableField<>();
    ObservableField<String> passwordAgain = new ObservableField<>();


    public static void startAction(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @SuppressWarnings("all")
    @Override
    protected void onStart() {
        super.onStart();
        //清除内容
        if (isRegister) {
            mBinding.topRigster.setTextColor(getResources().getColor(R.color.toptext_checked));
            mBinding.topRigster.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28);
            mBinding.topLogin.setTextColor(getResources().getColor(R.color.toptext));
            mBinding.topLogin.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
            mBinding.topLoginLine.setVisibility(View.GONE);
            mBinding.topRegisterLine.setVisibility(View.VISIBLE);
            mBinding.edtPasswordAgain.setVisibility(View.VISIBLE);
            mBinding.tvLogin.setText(R.string.register_tips);
        } else {
            Objects.requireNonNull(mBinding.topRigster).setTextColor(getResources().getColor(R.color.toptext));
            mBinding.topRigster.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
            mBinding.topLogin.setTextColor(getResources().getColor(R.color.toptext_checked));
            mBinding.topLogin.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28);
            mBinding.topLoginLine.setVisibility(View.VISIBLE);
            mBinding.topRegisterLine.setVisibility(View.GONE);
            mBinding.edtPasswordAgain.setVisibility(View.GONE);
            mBinding.tvLogin.setText(R.string.login_tips);
        }
    }

    @Override
    protected void initView() {

        mBinding.setPhone(phone);
        mBinding.setClick(new LoginClick());
//        mBinding.setCode(code);
        mBinding.setPassword(password);
        mBinding.setPasswordAgain(passwordAgain);


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initActivity() {
        super.initActivity();
    }


    @SuppressWarnings("all")
    public class LoginClick {

        //游客登录
        public void touristLogin(View view) {
            MainActivity.startAction(getContext());
            finish();
        }

        //切换成注册
        public void registerSwitch(View view) {
            if (!isRegister) {
                isRegister = true;
                onStart();
            }
        }

        //切换成登录
        public void loginSwitch(View view) {
            if (isRegister = true) {
                isRegister = false;
                onStart();
            }
        }

        //登录 and 注册
        public void login(View view) {
            if (isRegister) {
                //注册
                if (TextUtils.isEmpty(phone.get()) || TextUtils.isEmpty(password.get()) || TextUtils.isEmpty(passwordAgain.get())) {
                    ToastUtils.showToast(getContext(), "手机号或密码为空");
                } else if (!passwordAgain.get().equals(password.get())) {
                    ToastUtils.showToast(getContext(), "两次密码不一致");
                } else {
                    //网络请求注册
                    networkRegister();
                }
                //登录
            } else {
                if (TextUtils.isEmpty(phone.get()) || TextUtils.isEmpty(password.get())) {
                    ToastUtils.showToast(getContext(), "手机号或密码为空");
                } else {
                    //网路请求登录
                    networkLogin();
                }
            }
        }

        private void networkLogin() {
            LoginUserBean bean = new LoginUserBean(phone.get(), password.get());
            BaseRetrofitUtils.getInstance().create(Apis.class).login(bean)
                    .compose(new SchedulerTransformer<>())
                    .subscribe(new Consumer<LoginResponseBean>() {
                        @Override
                        public void accept(LoginResponseBean responseBean) throws Exception {
                            if (responseBean != null) {
                                if (responseBean.msg.equals("success")) {
                                    //获取其上的值，缓存密码
                                    SPUtil.getInstance().putData(GlobalConfig.user_Phone, phone.get());
                                    String encipherPassword = DigestUtils.generateSaltPassword(password.get());
                                    SPUtil.getInstance().putData(GlobalConfig.user_Password, encipherPassword);
                                    SPUtil.getInstance().putData(GlobalConfig.ysId, responseBean.ysId);//缓存医生Id
                                    //登录成功
                                    ToastUtils.showToast(getContext(), "登录成功");
                                    MainActivity.startAction(getContext());
                                    finish();
                                } else {
                                    ToastUtils.showToast(getContext(), responseBean.msg);
                                }
                            } else {
                                ToastUtils.showToast(getContext(), "返回值为空");
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            PopTip.tip("登录失败，" + throwable.getMessage());
                        }
                    });
        }

        private void networkRegister() {
            LoginUserBean bean = new LoginUserBean(phone.get(), password.get());
            BaseRetrofitUtils.getInstance().create(Apis.class).register(bean)
                    .compose(new SchedulerTransformer<>())
                    .subscribe(new Consumer<LoginResponseBean>() {
                        @Override
                        public void accept(LoginResponseBean responseBean) throws Exception {
                            if (responseBean.msg.equals("success")) {
                                //注册成功
                                ToastUtils.showToast(getContext(), "注册成功");
                            } else {
                                ToastUtils.showToast(getContext(), responseBean.msg);
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            PopTip.tip("注册失败，" + throwable.getMessage());
                        }
                    });
        }

    }
}
